<template>
	<view>
		<view class="body after-navber">
			<view style="display: flex; flex-direction: column; height: 100%; width: 100%">
				<view style="flex-grow: 0">
					<form>
						<view class="search-box">
							<view class="input-box">
								<view style="margin: 0 20rpx;">
									<input :autoFocus="true" @blur="inputBlur" @confirm="inputConfirm"
										@focus="inputFocus" class="search-input" confirmType="search"
										style="width: 100%" />
								</view>
							</view>
							<view>
								<view @tap="searchCancel" class="search-cancel">取消</view>
							</view>
						</view>
					</form>
				</view>
				<view style="flex-grow: 3; position: relative">
					<view class="search-history" v-if="show_history && recordlist && recordlist.length > 0">
						<view>
							<text class="search-history-title">搜索历史</text>
		 				<view @tap="clearWords" class="delete-search-history">
								<image src="/static/images/icon-delete.png" style="width: 28rpx; height: 34rpx"></image>
							</view>
						</view>
						<view class="search-history-list">
							<view @tap="historyClick" class="search-history-item" :data-value="item"
								v-for="(item, index) in recordlist" >
								{{ item }}
							</view>
						</view>
					</view>
					<block v-if="show_result">
						<view class="search-result" v-if="is_search">
							<view class="goods-list" style="padding-top: 20rpx">
								<navigator class="goods-item" openType="navigate" :url="'/pages2/shop/goods-detail/goods-detail?id='+item.id"
									v-for="(item, index) in goods_list" :key="item.id">
									<image class="goods-pic" mode="aspectFill" :src="item.image"></image>

									<view class="goods-info">
										<text class="goods-name">{{ item.name }}</text>
										<text class="goods-price">￥{{ item.price }}</text>
									</view>
								</navigator>
							</view>
	
						</view>
						<view class="search-result" v-else>
							<view class="flex-row flex-x-center" style="padding: 36rpx 0">
								<view class="flex-row flex-y-center">
									<view class="flex-grow-0"
										style="width: 100rpx; height: 100rpx; margin-right: 40rpx">
										<image src="/static/images/Cn3VLepkZwbG0iN9.png"
											style="width: 100rpx; height: 100rpx"></image>
									</view>
									<view class="flex-grow-1" style="color: #353535">抱歉，没有相关商品</view>
								</view>
							</view>
						</view>
					</block>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {Index} from '../../../apirequest/index-model.js';
	var i = new Index();
	export default {
		data() {
			return {
				show_history: true,
				show_result:false,
				is_search:false,
				recordlist: [],
				goods_list: [],
				has_more:true,
				page:1
			}
		},
		onLoad(options) {
			this.recordlist = uni.getStorageSync('recordlist') ? uni.getStorageSync('recordlist') : []
		},
		onReachBottom() {
			this._getList();
		},
		methods: {
			inputFocus: function() {
				this.show_history=true
				this.show_result=false
			},
			//清除历史搜索记录
			clearWords(){
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确认清空历史搜索记录吗?',
					success: function (res) {
						if (res.confirm) {
							uni.setStorageSync('recordlist',[])
							that.recordlist = [];
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				
			},
			inputBlur: function() {
				var that = this;

				if (0 < that.goods_list.length) {
					setTimeout(function() {
						this.show_history=false
						this.show_result=true
					}, 300);
				}
			},

			inputConfirm: function(t) {
				var that = this;
				var e = t.detail.value;
				if (0 != e.length) {
					this.keyword = e
					that.goods_list= [];
					that.has_more=true;
					that.page=1
					that.getGoodsList();
				}
			},

			searchCancel: function() {
				uni.navigateBack({
					delta: 1
				});
			},

			historyClick: function(t) {
				var a = t.currentTarget.dataset.value;

				if (0 != a.length) {
					this.keyword = a
					this.goods_list= [];
					this.has_more=true;
					this.page=1
					this.getGoodsList();
				}
			},
			getGoodsList: function() {
				var that = this;
				var keyword = this.keyword;
				if(!keyword || keyword == '' || keyword == null){
					uni.showToast({
						title:'请输入搜索内容',
						icon:'none'
					})
					return false;
				}
				var recordlist = uni.getStorageSync('recordlist') ? uni.getStorageSync('recordlist') : [];
				if(recordlist.indexOf(keyword) !== -1){
					recordlist.splice(recordlist.indexOf(keyword), 1);
				}
				recordlist = [keyword].concat(recordlist);
				if(recordlist.length > 10){
					recordlist.splice(10,1);
				}
				uni.setStorageSync('recordlist',recordlist)
				this.recordlist = recordlist;
				var page = this.page;
			
				if(!this.has_more){
					return;
				}
				i.get_product_list({keyword:keyword,page:page},(res) => {
					if(res.code == 1){
						var goods_list = that.goods_list;
						goods_list = goods_list.concat(res.data.data)
						that.goods_list = goods_list;
						that.page = page+1
						that.has_more=res.data.has_more
						this.show_history = false
						this.show_result = true
					
						if(that.goods_list.length > 0){
							this.is_search = true
						}
					}
				})
			

			}
		
		}
	}
</script>

<style>
	form {
		padding: 16rpx 24rpx;
		display: block;
	}

	.search-box {
		display: flex;
		flex-direction: row;
	}

	.search-box view {
		flex-grow: 1;
		align-items: center;
	}

	.search-box view:last-child {
		flex-grow: 0;
	}

	.cat {
		font-size: 11pt;
		margin: 0 10rpx 0 24rpx;
	}

	.pull-down {
		width: 124rpx;
	}

	.pull-down-list {
		background-color: #fff;
		position: absolute;
		top: 80rpx;
		width: 150rpx;
		border: 1px solid #e3e3e3;
		border-radius: 15rpx;
		z-index: 999;
	}

	.pull-down-text {
		font-size: 11pt;
		height: 88rpx;
		border-bottom: 1px solid #e3e3e3;
	}

	.active {
		color: #ff5c5c;
	}

	.input-box {
		display: flex;
		flex-direction: row;
		background: #fff;
		border: 1rpx solid #e3e3e3;
		height: 64rpx;
		border-radius: 10rpx;
	}

	.input-box view {
		flex-grow: 1 !important;
		align-items: center;
	}

	.input-box view:first-child {
		flex-grow: 0;
	}

	.search-box .search-icon {
		height: 24rpx;
		width: 24rpx;
	}

	.search-box .search-cancel {
		display: inline-block;
		height: 64rpx;
		line-height: 68rpx;
		color: #00c203;
		white-space: nowrap;
		padding: 0 24rpx;
		margin-right: -24rpx;
	}

	.search-history {
		padding: 24rpx 34rpx;
	}

	.search-history .search-history-title {
		color: #666666;
	}

	.search-history .delete-search-history {
		float: right;
		padding: 15rpx 20rpx;
		margin-top: -15rpx;
	}

	.search-history-list {
		padding: 24rpx 0 0 0;
	}

	.search-history-list .search-history-item {
		display: inline-block;
		height: 64rpx;
		line-height: 64rpx;
		padding: 0 32rpx;
		margin: 0 20rpx 20rpx 0;
		background: #ddd;
		border-radius: 10rpx;
	}

	.loading-bar {
		text-align: center;
		padding: 0;
		visibility: hidden;
		height: 0;
	}

	.loading-bar.active {
		visibility: visible;
		height: auto;
		padding: 20rpx;
	}

	.goods-list {
		margin-left: -10rpx;
		margin-right: -10rpx;
	}

	.goods-item {
		width: 365rpx;
		display: inline-block;
		position: relative;
		margin: 0 10rpx;
		margin-bottom: 20rpx;
		font-size: 0;
		background: #fff;
	}

	.goods-item .goods-pic {
		width: 100%;
		height: 365rpx;
	}

	.goods-item .goods-info {
		padding: 5rpx 0;
	}

	.goods-item .goods-name {
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
		padding: 10rpx;
		font-size: 9pt;
		display: block;
		text-align: center;
	}

	.goods-item .goods-price {
		font-size: 11pt;
		color: #f40;
		display: block;
		text-align: center;
	}

	.loading-more-bar {
		text-align: center;
		margin-bottom: 20rpx;
		opacity: 0;
	}

	.loading-more-bar.active {
		opacity: 1;
	}
</style>
